package com.example.rabbitmqdemo.config;

import org.springframework.amqp.core.*;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE_NAME_1 = "test_queue_1";
    public static final String QUEUE_NAME_2 = "test_queue_2";
    public static final String EXCHANGE_NAME = "test_exchange";
    public static final String ROUTING_KEY_1 = "test.routing.key.1";
    public static final String ROUTING_KEY_2 = "test.routing.key.2";
    public static final String VIP_QUEUE="vip_queue";
    public static final String VIP_ROUTER_KEY="vip.router.key";
    public static final String DLX_QUEUE="dlx_queue";
    public static final String DLX_ROUTER_KEY="dlx.router.key";
    public static final String DELIVERY_QUEUE="delayed_queue";
    @Bean
    public Queue queue() {
        return QueueBuilder.durable(QUEUE_NAME_1).withArgument("x-max-priority",10).build();
    }
    @Bean
    public Queue queue2() {
        return QueueBuilder.durable(QUEUE_NAME_2).withArgument("x-max-priority",10).build();
    }
    @Bean
    public Queue vipQueue() {
       return QueueBuilder.durable(VIP_QUEUE)
               .withArgument("x-max-priority", 10)
               .build();
    }
    @Bean
    public Queue dlxQueue() {
        return new Queue(DLX_QUEUE, true, false, false);
    }
    @Bean
    public Queue delayedQueue() {
        return QueueBuilder.durable(DELIVERY_QUEUE)
                .withArgument("x-dead-letter-exchange", DELIVERY_QUEUE )
                .withArgument("x-dead-letter-routing-key", ROUTING_KEY_1)
                .withArgument("x-message-ttl", 1000)
                .build();
    }
    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding1() {
       return BindingBuilder.bind(queue()).to(exchange()).with(ROUTING_KEY_1);
    }
    @Bean
    public Binding binding2() {
        return BindingBuilder.bind(queue2()).to(exchange()).with(ROUTING_KEY_2);
    }
    @Bean
    public Binding vipBinding() {
        return BindingBuilder.bind(vipQueue()).to(exchange()).with(VIP_ROUTER_KEY);
    }



    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(new Queue(DLX_QUEUE, true, false, false))
                .to(exchange()).with(DLX_ROUTER_KEY);
    }

    @Bean
    public Binding delayedQueueBinding() {
        return BindingBuilder.bind(delayedQueue()).to(exchange()).with("delayed");
    }


//    @Bean
//    public Queue queueWithDlx() {
//        // 设置死信交换机与路由
//        return QueueBuilder.durable(QUEUE_NAME)
//                .withArgument("x-dead-letter-exchange", "test_exchange")
//                .withArgument("x-dead-letter-routing-key", "dlx_routing_key")
//                .build();
//    }

}